#include "ULUI_LookupSingle.h"
#include "ULUI_LookupRange.h"
#include "ULUI_LookupLatest.h"
#include "ULUI_LookupLatestBlue.h"
#include "ULUI_UpdateTxt.h"
#include "ULUI_StatisticFinal.h"
#include "ULUI_StatisticOut.h"
#include "ULGfx_Statistic.h"
#include "UL_Global.h"
#include "UL_Tools.h"

#define UPDATE_TXT_START_X	100
#define UPDATE_TXT_START_Y	100

ULUI_UpdateTxt ULUI_UpdateTxt::updateTxt;

enum TxtLookupIDs{
	EXIT,	
};	

static char *menuTitles[] = {
	"返回"
};

void ULUI_UpdateTxt::Init()
{
	int i;
	VSDL_Point currentXY(UPDATE_TXT_START_X, UPDATE_TXT_START_Y);
	int w, h;
	ULUI_Menu menu;
	int ret;

	AttachEventHandler(this);

	menu.SetFont(UL_Global::UnionLottoFont());
	menu.ShowBoarder(true);
	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}

	menus.SetFirstMenuXY(VSDL_Point(UPDATE_TXT_START_X, UPDATE_TXT_START_Y));
	menu.GetTitleSize(w, h);
	
	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	UL_Global::UnionLottoShowTip("连接服务器(www.17500.cn)...", currentXY);
	UL_Global::UnionLottoUpdateScreen();
	currentXY.y += h;
       	UL_Global::UnionLottoShowTip("下载文件...", currentXY);
	UL_Global::UnionLottoUpdateScreen();
	currentXY.y += h;
	ret = UL_Tools::GetHistoryFileFromWebSite("history.txt");

	if (ret < 0){
       		UL_Global::UnionLottoShowTip("下载文件失败", currentXY);
		UL_Global::UnionLottoUpdateScreen();
		currentXY.y += h;
		updateResult = false;
		return ;	
	}

       	UL_Global::UnionLottoShowTip("下载文件成功", currentXY);
	UL_Global::UnionLottoUpdateScreen();
	currentXY.y += h;
	updateResult = true;
}

void ULUI_UpdateTxt::CleanUp()
{
	menus.Reset();
}

void ULUI_UpdateTxt::Pause()
{
}

void ULUI_UpdateTxt::Resume()
{
	selectedMenu = -1;
}

void ULUI_UpdateTxt::Update(VSDL_GameEngine *game)
{
	if (selectedMenu == -1){
		return ;
	}

	switch (selectedMenu){
		case EXIT:
			game->PopState();
			break;
	}
}

void ULUI_UpdateTxt::Draw(VSDL_GameEngine *game)
{
	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(game->GetSurface());
	int fontHeight = UL_Global::UnionLottoGetFontHeight();

	if (updateResult){
		UL_Global::UnionLottoShowTip("更新TXT文件成功，点击返回到上一级菜单", VSDL_Point(UPDATE_TXT_START_X, UPDATE_TXT_START_Y + fontHeight));
	}else{
		UL_Global::UnionLottoShowTip("更新TXT文件失败，点击返回到上一级菜单", VSDL_Point(UPDATE_TXT_START_X, UPDATE_TXT_START_Y + fontHeight));
	}
}

ULUI_UpdateTxt *ULUI_UpdateTxt::Instance()
{
	return &updateTxt;
}


void __HANDLE_EVENT ULUI_UpdateTxt::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	if (keyStates[SDLK_ESCAPE]){
		game->PopState();
	}
}

void __HANDLE_EVENT ULUI_UpdateTxt::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	menus.UpdateMenuList(absX, absY);
}

void __HANDLE_EVENT ULUI_UpdateTxt::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	int i;
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;
		}
	}
	
}

void __HANDLE_EVENT ULUI_UpdateTxt::OnQuit(void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	game->Quit();
}
